<template>
    <div class="matchground">
        <div class="row">
            <div class="col-4">
                <div class="user_photo">
                    <img :src="$store.state.user.photo" alt="头像">
                </div>
                <div class="user_username">
                    {{ $store.state.user.username }}
                </div>
            </div>
            <div class="col-4">
                <div class="user_select_snake">
                    <select v-model="select_snake" class="form-select" aria-label="Default select example">
                        <option value="-1" selected>玩家</option>
                        <option v-for="snake in snakes" :key="snake.id" :value="snake.id">{{ snake.title }}</option>
                    </select>
                </div>
            </div>
            <div class="col-4">
                <div class="user_photo">
                    <img :src="$store.state.pk.opponent_photo" alt="头像">
                </div>
                <div class="user_username">
                    {{ $store.state.pk.opponent_username }}
                </div>
            </div>

            <div class="col-12">
                <button type="button" class="btn btn-info btn-lg" @click="click_match_btn">{{ match_btn_info }}</button>
            </div>
        </div>
    </div>
</template>

<script>
import { ref } from 'vue'
import { useStore } from 'vuex'
import $ from 'jquery'

export default {
    setup(){

        const store = useStore();
        let match_btn_info = ref("开始匹配");
        let snakes = ref([]);
        let select_snake = ref("-1");

        const click_match_btn = () => {
            if(match_btn_info.value === "开始匹配"){
                match_btn_info.value = "取消";
                store.state.pk.socket.send(JSON.stringify({
                    event: "start_matching",
                    snake_id: select_snake.value
                }));
            } else {
                match_btn_info.value = "开始匹配";
                store.state.pk.socket.send(JSON.stringify({
                    event: "stop_matching"
                }));
                
            }
        }

         const refresh_snakes = () => {
            $.ajax({
                url: "http://localhost:8090/user/snake/query",
                type: "get",
                headers: {
                    Authorization: "Bearer " + store.state.user.token
                },
                success(resp){
                    snakes.value = resp;
                    
                }
            })
        }

        //从云端动态获取snake列表
        refresh_snakes();

        return {
            match_btn_info,
            click_match_btn,
            snakes,
            select_snake
        }
    }
}

</script>

<style scoped>
.matchground{
    width: 60vw;
    height: 70vh;
    margin: 40px auto;
    background-color: rgba(50,50,50,0.5);
}

.user_photo{
    text-align: center;
    padding-top: 10vh;
}

.user_photo > img {
    border-radius: 50%;
    width: 20vh;
}

.user_username {
    padding-top: 5vh;
    text-align: center;
    font-size: 24px;
    font-weight: 600;
    color: white;
}

.col-12{
    margin-top: 5vh;
    text-align: center;
}

button{
    height: 10vh;
    border-radius: 50%;
}

.user_select_snake{
    padding-top: 20vh;
}
.user_select_snake > select{
    width: 60%;
    margin: 0 auto;
}
</style>